(function () {
  class TyphoonPolygonLayer extends BaseLayer {
    constructor(config = {}) {
      super(config);
      this.minZoom = 0
      this.maxZoom = 21
      this.name =  'TyphoonPolygonLayer'
      this.noneTips = true
      this.initLayer({
        type: 'fill',
        'paint': {
          'fill-color': ['get', 'fillColor'],
          'fill-opacity': ['get', 'fillOpacity'],
          'fill-outline-color': ['get', 'outline'],
        }
      })

    }


    getStyle(item) {
      item = Object.assign({}, item, {
        fillColor: item.fillColor || '#ff0000',
        fillOpacity: .5,
        outline: item.outline
      })
      return item
    }

    makeFeature(item) {
      item = this.getStyle(item)
      let f = {
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: item.polygonCoor
        },
        properties: {
          icon: item.icon,
          ...item,
          data: item,
        }
      }
      return f
    }

     getTyphoonCircle(radiusData, center) {
      const centerPoint = turf.point(center); // 台风中心点
      const options = { steps: 64 }; // 设置精度

      // 根据四个方向的半径生成弧线
      const arcNE = turf.lineArc(centerPoint, radiusData.ne, 0, 90, options);
      const arcSE = turf.lineArc(centerPoint, radiusData.se, 90, 180, options);
      const arcSW = turf.lineArc(centerPoint, radiusData.sw, 180, 270, options);
      const arcNW = turf.lineArc(centerPoint, radiusData.nw, 270, 360, options);

      // 合并弧线生成完整的多边形
      const combinedCoords = [
        ...arcNE.geometry.coordinates,
        ...arcSE.geometry.coordinates,
        ...arcSW.geometry.coordinates,
        ...arcNW.geometry.coordinates,
        arcNE.geometry.coordinates[0] // 闭合多边形
      ];
      return turf.polygon([combinedCoords]);
    }

    loadData(item) {
      this.cacheData = []
      if (!item) return

      if (item.radius7) {
        let radius7_quad = item.radius7_quad
        let polygonFea = this.getTyphoonCircle(radius7_quad, [item.lng, item.lat])
        this.cacheData.push( {
          name: '七级风圈',
          polygonCoor: polygonFea.geometry.coordinates,
          // 浅绿
          fillColor: 'rgba(0,255,0,0.5)',
          outline: 'rgba(0,255,149,0.5)'
        },)
      }

      if (item.radius10) {
        let radius10_quad = item.radius10_quad
        let polygonFea = this.getTyphoonCircle(radius10_quad, [item.lng, item.lat])

        this.cacheData.push( {
          name: '十级风圈',
          polygonCoor: polygonFea.geometry.coordinates,
          // 浅蓝
          fillColor: 'rgba(0,0,255,0.5)',
          outline: 'rgba(5,5,165,0.5)'
        },)
      }

      if (item.radius12) {
        let radius12_quad = item.radius12_quad
        let polygonFea = this.getTyphoonCircle(radius12_quad, [item.lng, item.lat])

        this.cacheData.push( {
          name: '十二级风圈',
          polygonCoor: polygonFea.geometry.coordinates,
          // 浅红
          fillColor: 'rgba(255,0,0,0.5)',
          outline: 'rgba(255,27,27,0.5)'
        },)
      }

      this.refreshLayer()
    }
  }


  window.addEventListener('mapCreatedEvent', ev => {
    new TyphoonPolygonLayer({
      name: 'TyphoonPolygonLayer'
    })
  })
})();
